package com.monkeygroup.mygoplus.view.home.adapter;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.common.utils.image.Images;
import com.monkeygroup.mygoplus.thing.vo.RentVO;
import com.monkeygroup.mygoplus.view.home.activity.SellHouseActivity;

//import com.facebook.drawee.view.SimpleDraweeView;

public class SellHousePageAdapter extends ArrayAdapter<RentVO> {
	private final String TAG = "SellHousePageAdapter";
	private int resourceId;
	private Context context;
	private boolean isAllItemEnable=true;

	public SellHousePageAdapter(Context context, int resourceId) {
		super(context, resourceId);
		this.context = context;
		this.resourceId = resourceId;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final RentVO vo = getItem(position);
		View view = null;
		final ItemView itemView;
		if(convertView == null){
			itemView = new ItemView();
			view = LayoutInflater.from(getContext()).inflate(resourceId, null);
			itemView.houseImg = (ImageView)view.findViewById(R.id.img_sell_house_item);
			itemView.areaName = (TextView)view.findViewById(R.id.txt_sell_house_item_area);
			itemView.houseName = (TextView)view.findViewById(R.id.txt_sell_house_item_name);
			itemView.housePrice = (TextView)view.findViewById(R.id.txt_sell_house_item_price);
			itemView.houseAddr = (TextView)view.findViewById(R.id.txt_sell_house_item_address);
			view.setTag(itemView);
		} else {
			view = convertView;
			itemView = (ItemView)view.getTag();
		}

		Images.with(context).load(Uri.parse(vo.getImg())).resize(130,90).into(itemView.houseImg);

		itemView.houseName.setText(vo.getName());
		if(vo.getRentprice()!=0){
			itemView.housePrice.setText(vo.getRentprice()+"元");
		}

		if(vo.getSaleprice()!=0){

			itemView.housePrice.setText(vo.getSaleprice()+"萬");
		}

		if(vo.getUnitprice()!=0){
			itemView.housePrice.setText(vo.getUnitprice()+"萬/每平米");
			itemView.areaName.setVisibility(View.GONE);
		}

		if(vo.getUnitprice1()!=0&&vo.getUnitprice2()!=0){
			itemView.housePrice.setText(vo.getUnitprice1()+"~"+vo.getUnitprice1()+"萬");
		}
		if(vo.getLawnarea()!=null){
			itemView.areaName.setText(vo.getLawnarea()+"坪");
		}
		else if("1".equals(vo.getType())){

			itemView.areaName.setText("预售屋");
		}
		else if("3".equals(vo.getType())){

			itemView.areaName.setText("新成屋");
		}
		else if("4".equals(vo.getType())){

			itemView.areaName.setText("建构中");
		}

		if(!"".equals(vo.getStreetname())){
			itemView.houseAddr.setText(vo.getStreetname());
		}
		if(!"".equals(vo.getAddr())){
			itemView.houseAddr.setText(vo.getAddr());
		}

		return view;
	}
	
	@Override
	public long getItemId(int position) {
		return super.getItemId(position);
	}
	
	@Override
	public int getCount() {
		return super.getCount();
	}
	
	@Override
	public RentVO getItem(int position) {
		return super.getItem(position);
	}

	@Override
	public boolean isEnabled(int position) {
		return isAllItemEnable;
	}

	public void disableAllItemChoser() {
		isAllItemEnable = false;
		notifyDataSetChanged();
	}
	public void enableItemChoser() {
		isAllItemEnable = true;
		notifyDataSetChanged();
	}

	class ItemView {
		ImageView houseImg;
		TextView houseName;
		TextView housePrice;
		TextView areaName;
		TextView houseAddr;
	}

}
